﻿#ifndef OBJECTFACTORY_H
#define OBJECTFACTORY_H

#include "ObjLib_global.h"

namespace xvs {


typedef QObject* (*Constructor)();

class OBJLIB_EXPORT ObjectFactory
{
public:
    ObjectFactory();

    static QObject *createObject(const QString& className,bool isObject);

    static void registerClass(const QString& className,bool isRegisterObject,Constructor constructor);

    static QStringList registerClassNames(bool isObject);
private:    
    static QObject *createObjectFactory(const QString& className, bool isObject);

    inline static QMap<QString , Constructor>& constructors();

    inline static QMap<QString , Constructor>& var_constructors();
};

//宏方法声明
#define DECLARE_XVS_OBJECT(class_name) \
class class_name##Helper; \
extern class_name##Helper class_name##helper; \

//宏方法注册类
#define IMPL_XVS_OBJECT(class_name) \
class class_name##Helper{ \
public: \
    class_name##Helper()\
    { \
        ObjectFactory::registerClass(#class_name,true,class_name##Helper::createObjectFunc); \
    } \
    static QObject* createObjectFunc(){ \
        return new class_name(); \
    } \
}; \
class_name##Helper class_name##helper; \

//宏方法注册数据
#define IMPL_XVS_VAR(class_name) \
class class_name##Helper{ \
public: \
    class_name##Helper()\
    { \
        ObjectFactory::registerClass(#class_name,false,class_name##Helper::createObjectFunc); \
    } \
    static QObject* createObjectFunc(){ \
        return new class_name(); \
    } \
}; \
class_name##Helper class_name##helper; \

}//end namespace xvs

#endif // OBJECTFACTORY_H
